//
//  MovieListViewController.m
//  UI_豆瓣2.0
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListCell.h"
#import "MovieDetailViewController.h"
#import "MJRefresh.h"
#import "AppTools.h"
#import "MovieList.h"
#import "DataBaseSingleton.h"

@interface MovieListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *movieTableView;
@property (nonatomic, retain)NSMutableArray *movieListArray;
@property (nonatomic, assign) BOOL isUpLoad;
@property (nonatomic, assign) BOOL isSelected;

@property(nonatomic,retain)UIButton *button;

@end

@implementation MovieListViewController

- (void)dealloc
{
    [_movieListArray release];
    [_movieTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.movieTableView];
    
#pragma mark -- 注册cell
    [self.movieTableView registerClass:[MovieListCell class] forCellReuseIdentifier:@"cell"];
#pragma mark -- 去掉cell的线
    self.movieTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
#pragma mark -- 调用上拉下拉方法
    [self addHeaderRefresh];
    [self addFooterRefresh];
// 创建接收数组
    self.movieListArray = [NSMutableArray array];
#pragma mark -- 数据库查询
    NSMutableArray *array = [DATABASE selectMovie];
    self.movieListArray = [NSMutableArray arrayWithArray:array];
    [self.movieTableView reloadData];
// 默认不是上拉
    self.isUpLoad = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.allMovieArray = [NSMutableArray array];
    self.searchArray = [NSMutableArray array];
    
    
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    //    设置开始搜索时背景显示与否 页面背景阴影效果,一般不要设置.
    self.searchController.dimsBackgroundDuringPresentation = NO;
    //设置点击搜索框时候隐藏导航栏
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    //    [searchBar sizeToFit]:设置searchBar位置自适应
    [self.searchController.searchBar sizeToFit];
    
    self.movieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.movieTableView];

    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    self.movieTableView.tableHeaderView = self.searchController.searchBar;
    
    
    self.allMovieArray = [NSMutableArray arrayWithArray:self.movieListArray];
    [_movieTableView release];
    
}



#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    [self.movieTableView addHeaderWithCallback:^{
        
        NSLog(@"下拉");
        self.isUpLoad = NO;
        // 重新请求数据
        [self getAFNetWorkingData];
        
    }];
    // 进到应用自动刷新(可以放在viewdidload)
    [self.movieTableView headerBeginRefreshing];
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    //当上拉时候触发该方法
    [self.movieTableView addFooterWithCallback:^{
        
        NSLog(@"上拉");
        self.isUpLoad = YES;
        // 重新请求数据
        [self getAFNetWorkingData];
    }];
}


#warning Block封装AFN,请求数据,并将数据存入model中
- (void)getAFNetWorkingData{
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"];
    [AppTools getDataWithURL:url_string Block:^(id result) {
        
        if (self.isUpLoad == NO) {
            [self.movieListArray removeAllObjects];
            NSLog(@"清空数组");
        } else {
            NSLog(@"不用清空数组");
        }
        NSMutableDictionary *bigDic = result;
        NSMutableArray *array = [bigDic objectForKey:@"result"];
        for (NSMutableDictionary *dic in array) {
            MovieList *movieL = [[MovieList alloc] init];
            [movieL setValuesForKeysWithDictionary:dic];
            [self.movieListArray addObject:movieL];
            [DATABASE deleteMovieId:movieL.movieId];
            [DATABASE insertMovie:movieL];
            [movieL release];
        }
#pragma mark -- 结束下拉上拉刷新
        [self.movieTableView headerEndRefreshing];
        [self.movieTableView footerEndRefreshing];
        [self.movieTableView reloadData];
    }];
}

#pragma mark -- TableView懒加载
-(UITableView *)movieTableView{
    if (_movieTableView == nil) {
        _movieTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 110) style:UITableViewStylePlain];
        self.movieTableView.delegate = self;
        self.movieTableView.dataSource = self;
    }
    return _movieTableView;
}
#pragma mark -- 设置区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark -- 设置行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.searchController.active) {
        
        return self.searchArray.count;
    }
    else {
        return self.allMovieArray.count;
    }

}
#pragma mark -- 设置Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdntifier = @"cell";
    MovieListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdntifier];
    
    
    if (cell == nil) {
        
        cell = [[MovieListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdntifier];
    }
    if (self.searchController.active) {
        MovieList *movieL = [self.searchArray objectAtIndex:indexPath.row];
        cell.movieLCell = movieL;
    }
    else {
        MovieList *movieL = [self.allMovieArray objectAtIndex:indexPath.row];
        cell.movieLCell = movieL;
    }

    
#warning 属性传值
    
    
    return cell;
}
#pragma mark -- 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
#pragma mark -- 点击cell触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
// 隐藏详情界面的TabBar
    MovieDetailViewController *movieDetailVC = [[MovieDetailViewController alloc] init];
    [movieDetailVC setHidesBottomBarWhenPushed:YES];
    
    
    
    if (self.searchController.active) {
        
        MovieList *movieL = [self.movieListArray objectAtIndex:indexPath.row];
        movieDetailVC.movieL = movieL;
    }
    else {
        
        MovieList *movieL = [self.movieListArray objectAtIndex:indexPath.row];
        movieDetailVC.movieL = movieL;
    }

    
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    [movieDetailVC release];
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSString *searchString = [self.searchController.searchBar text];
    
    [self.searchArray removeAllObjects];
    for (MovieList *movieL in self.movieListArray) {
        if ([movieL.movieName rangeOfString:searchString].location != NSNotFound) {
            [self.searchArray addObject:movieL];
        }
    }
    [self.movieTableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
